Networking & Communications

This week we working on the communications between different node on a project, it's perfect i have multiple nodes on mine.

The ESP board :

The hearts of this project is a ESP8266 board, this board going on a API to take the weather information & after transmit this informations on the different module.
She also received information from a Temperature & humidity sensor board for the room information.

For Send the information to the other nodes i use a a synchronous Serial bus based on a RS-232 protocole.

This part use the Wifi communication to get all the weather data and use the ArduinoJson librarie for translate it for the arduino (here i use the V5)

						

void getWeatherData()                                //client function to send/receive GET request data.
   {
   if (client.connect(servername, 80))
          {                                         //starts client connection, checks for connection
          client.println("GET /data/2.5/weather?zip="+Zip+","+Country+"&units=metric&appid="+APIKEY);
          client.println("Host: api.openweathermap.org");
          client.println("User-Agent: ArduinoWiFi/1.1");
          client.println("Connection: close");
          client.println();
          }
  else {
         Serial.println("connection failed");        //error message if no client connect
          Serial.println();
       }

  while(client.connected() && !client.available())
  delay(1);                                          //waits for data
  while (client.connected() || client.available())
       {                                             //connected or data available
         char c = client.read();                     //gets byte from ethernet buffer
         result = result+c;
       }

  client.stop();                                      //stop client
  result.replace('[', ' ');
  result.replace(']', ' ');
  Serial.println(result);
  char jsonArray [result.length()+1];
  result.toCharArray(jsonArray,sizeof(jsonArray));
  jsonArray[result.length() + 1] = '\0';
  StaticJsonBuffer<1024> json_buf;
  JsonObject &root = json_buf.parseObject(jsonArray);

  if (!root.success())
  {
    Serial.println("parseObject() failed");
  }

  int id = root["weather"]["id"];
  const char* description = root["weather"]["description"];
  float main_temp = root["main"]["temp"];
  int main_humidity = root["main"]["humidity"];
  int clouds_all = root["clouds"]["all"];
  long dt = root["dt"];
  long sys_sunrise = root["sys"]["sunrise"];
  long sys_sunset = root["sys"]["sunset"];
  const char* cityName = root["name"];

					
					

Here is the part of the code i use to create a information Buffer & sending it on the Serial Bus.

						

	void sendStepper( int message1 , int message2 , int message3 , int message4 , int message5 ){

	  sendBuffer[0] = 'b';   // Byte that begin the communication
	  sendBuffer[1] = highByte( message1 );
	  sendBuffer[2] = lowByte( message1 );
	  sendBuffer[3] = highByte( message2 );
	  sendBuffer[4] = lowByte( message2 );
	  sendBuffer[5] = message3;
	  sendBuffer[6] = message4;
	  sendBuffer[7] = message5;
	  sendBuffer[8] = '\n';   // Byte that end the communication

	  for (int i = 0; i <= 8; i++)
	  {
	    Serial.write(sendBuffer[i]);
	  }
	}

						
					

here you could show the FULL CODE

The Stepper Node:

One of the node is made to move one of the dispaly gear with a stepper and control the LED (on the final project i put one of this module for each of the 3 gears for my full display.)
For this module i use a ATTiny 1614 for control it.

I connect on this board a small sensor part and the LED Strip

Here is the part of the Stepper modules code i use to received the data send by the ESP:

						
			void readSerial()
			{
			  if (Serial.available() > 0)
			  {
			    Serial.readBytesUntil('\n',messageR,9);
			    if ( messageR[0] == byte('a'))
			    {
			      weatherID = messageR[1] << 8 + messageR[2];
			      DayOrNight = messageR[3];
			    }
			  }
			}
						
					

here you could find the Full code of the first stepper module.

The sensor board:

This board use a Si7020 Sensor.
This Temperature & humidity sensor communicate with the ATtiny1614 on the board with a I2C communication protocole.

The Board :

I decide to put the sensor on the bottom of the board for a better integration on my futur project.

The I2C communication:

According with the Data-sheet of the sensor here is the code i use to get the information:

					
void SensorRead()
	{
	unsigned int data[2];

	// Start I2C transmission
	Wire.beginTransmission(Addr);
	// Send Humidity measurement command, NO HOLD MASTER
	Wire.write(0xF5);
	// Stop I2C transmission
	Wire.endTransmission();
	delay(500);

	// Request 2 bytes of data
	Wire.requestFrom(Addr, 2);

	// Read 2 bytes of data
	// Humidity msb, Humidity lsb
	if(Wire.available() == 2)
	{
	  data[0] = Wire.read();
	  data[1] = Wire.read();
	}

	// Convert the data
	Humidity  = ((data[0] * 256.0) + data[1]);
	Humidity = ((125 * Humidity) / 65536.0) - 6;

	// Start I2C transmission
	Wire.beginTransmission(Addr);
	// Send temperature measurement command, NO HOLD MASTER
	Wire.write(0xF3);
	// Stop I2C transmission
	Wire.endTransmission();
	delay(500);

	// Request 2 bytes of data
	Wire.requestFrom(Addr, 2);

	// Read 2 bytes of data
	// temp msb, temp lsb
	if(Wire.available() == 2)
	{
	  data[0] = Wire.read();
	  data[1] = Wire.read();
	}

	// Convert the data
	temp  = ((data[0] * 256.0) + data[1]);
	ctemp = ((175.72 * temp) / 65536.0) - 46.85;  // convert temp data in a celsius temp.

	Temp = round( ctemp *100 );

	delay(1000);
}
					
				  

Here you could find the Full Code with the communication with the ESP board

Ok for the theorical parts......

But after the Shut down of the lab, i could now have access again to the Fablab, but with only a month to put into the reality everything i theorize and design at home.

Let's start the sprint times......

With the time i have to make my final project & the different missing assignement i design, i have to make some choice. After i fabricate a first prototype of my boards and programming it i encounter a big difficulty, they don't want to communicate each other like i want. I try to debug it and the first problem is : Separatly each board work well but when i try to received information from the Esp board on the ATtiny one the information received was totaly no readable....

After a lot a debugging, reflexions i decide to simplify the communication between boards.

Now on the projectt i have only one way communication with a single charachter to transmit, all the interpretations and the screen programation would be on the Esp directly.

							
void SendtoMecha()
{
Serial.write(getMechaId(current->id, true));
}


char getMechaId(uint16_t id, bool today)
{
if ( today && id/100 == 8 && (current->dt < current->sunrise || current->dt > current->sunset)) id += 1000;
if ( today && id/100 == 5 && (current->dt < current->sunrise || current->dt > current->sunset)) id += 1000;
if ( today && id/100 == 6 && (current->dt < current->sunrise || current->dt > current->sunset)) id += 1000;

if (id/100 == 2) return 'a';
if (id/100 == 3) return 'b';
if (id == 500) return 'c';
if (id == 501) return 'c';
if (id == 502) return 'c';
if (id == 503) return 'c';
if (id == 504) return 'c';
if (id == 511) return 'd';
else if (id/100 == 5) return 'e';
if (id == 600) return 'f';
if (id >= 611 && id <= 616) return 'd';
else if (id/100 == 6) return 'g';
if (id/100 == 7) return 'h';
if (id == 800) return 'i';
if (id == 801) return 'j';
if (id == 802) return 'k';
if (id == 803) return 'l';
if (id == 804) return 'l';
if (id == 1800) return 'm';
if (id == 1801) return 'n';
if (id == 1802) return 'k';
if (id == 1803) return 'l';
if (id == 1804) return 'l';
if (id == 1500) return 'o';
if (id == 1501) return 'o';
if (id == 1502) return 'o';
if (id == 1503) return 'o';
if (id == 1504) return 'o';
if (id == 1511) return 'd';
if (id == 1600) return 'p';
if (id >= 1611 && id <= 1616) return 'd';
else if (id/100 == 16) return 'g';
}
							
						

Finaly after some long crisis debugging. I finally find the problem. For make my LED array working with the classic adafruit library i have to clocked my Attiny1614 to 16mhz, but i find a library make specially by space & konde to program neo pixel with a 20mhz clock. I do the change with this new one (easy opperations, he make this tinyNeopixel library looking same as the adafruit one ^^).
Now Boards communicate and make what i want they do....

You could find more informations (codes & files) on the development of the communication & of each boards on my final project diary here

On this assignement, i discovered that communication could be hard understanding & could create hard debugging crisis, but i could find the solutions & sometimes the solutions is not one the communication properly ^^